#include <p_std.h>
#include <p_file.h>
#include <p_sys.h>
#include <epoc.h>

GLREF_D VOID *DatCommandPtr;

LOCAL_D VOID *fcb;      /* handle of resource file */
LOCAL_D UWORD resoff;   /* offset to resource file within .img file */
LOCAL_D UWORD ixpos;    /* offset to index table within resource file */

LOCAL_D TEXT buf[80];   /* scratch buffer used for resources loaded */

LOCAL_C VOID SeekToPos(UWORD roff)
/*
Position to offset roff within the resource file
*/
    {
    LONG ioff;         /* offset within image file */

    ioff=resoff+roff;
    p_seek(fcb,P_FABS,&ioff);
    }

LOCAL_C VOID InitRsc(VOID)
/*
Open resource file at add-file slot 2 inside own .img file.
Set up the values of fcb, resoff, and ixpos
*/
    {
    ImgHeader head;

    p_open(&fcb,DatCommandPtr,P_FRANDOM|P_FSTREAM|P_FSHARE);
    p_read(fcb,&head,sizeof(head));
    resoff=head.Add[1].offset;
    SeekToPos(0);
    p_read(fcb,&ixpos,2);
    }

LOCAL_C TEXT *ReadRsc(INT i)
/*
Returns pointer to buffer containing resource i.
Note that the static buffer buf[] is used in every case.
*/
    {
    UWORD tmp[2];               /* section of index table */

    SeekToPos(ixpos+((i-1)*2)); /* position into index table */
    p_read(fcb,&tmp[0],4);
    SeekToPos(tmp[0]);
    p_read(fcb,&buf[0],tmp[1]-tmp[0]);
    return(&buf[0]);
    }

GLDEF_C INT main(VOID)
    {
    InitRsc();
    p_printf("Resource 1 is %s",ReadRsc(1));
    p_printf("Resource 2 is %s",ReadRsc(2));
    p_printf("Press any key to exit");
    p_getch();
    return(0);
    }
